using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BaseDatos;

namespace SCHD
{
    public partial class WFConfColumnas : TDV.Controles.Base.WinForm.BaseForm_Tipo01
    {
        public WFConfColumnas()
        {
            InitializeComponent();
        }
        private LBColumna ColumMod = new LBColumna();
        private bool elimina = false;
        private void WFConfColumnas_Load(object sender, EventArgs e)
        {
            Cargar();
            CargarColumnas();
        }
        protected void CargarColumnas()
        {
            try
            {
                GrdColumnas.DataSource = null;
                LBColumna col = new LBColumna();
                col.Filtros.CodigoAtributo = "";
                col.Buscar();
                GrdColumnas.DataSource = col;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        protected void Cargar()
        {
            try
            {
            GrdAtributos.DataSource = null;
            LBAtributo atributo = new LBAtributo();
            atributo.Filtros.Opcion = "QRY04";
            atributo.Buscar();
            if (atributo.Count > 0)
            {
                GrdAtributos.DataSource = atributo;
            }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void BtnSalir_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BtnDerecha_Click(object sender, EventArgs e)
        {
            elimina = true;
            
            try
            {
                foreach (DataGridViewRow row in GrdAtributos.Rows)
                {
                    DataGridViewCheckBoxCell ck1 = row.Cells[0] as DataGridViewCheckBoxCell;

                    //CheckBox ck = (CheckBox)row.Cells[0].Value;
                    if (Convert.ToBoolean(ck1.Value))
                    {
                        // Asignar Columna
                        EBColumna colum = new EBColumna();
                        colum.Orden = 0;
                        colum.CodigoAtributo = row.Cells[1].Value.ToString();
                        colum.Nombres = row.Cells[2].Value.ToString();
                        colum.Grabar();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            Cargar();
            CargarColumnas();
        }

        private void BtnIzquierda_Click(object sender, EventArgs e)
        {
            elimina = true;
            try
            {
                foreach (DataGridViewRow row in GrdColumnas.Rows)
                {
                    DataGridViewCheckBoxCell ck1 = row.Cells[0] as DataGridViewCheckBoxCell;

                    //CheckBox ck = (CheckBox)row.Cells[0].Value;
                    if (Convert.ToBoolean(ck1.Value))
                    {
                        EBColumna co = new EBColumna();
                        co.CodigoAtributo = row.Cells[2].Value.ToString();
                        co.Eliminar();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            Cargar();
            CargarColumnas();
        }

        private void BtnAceptar_Click(object sender, EventArgs e)
        {
            if (elimina == false)
            {

                foreach (EBColumna _Columna in ColumMod)
                {
                    _Columna.Grabar();
                }
                ColumMod.Clear();
            }
            DialogResult = DialogResult.OK;
        }

        private void GrdColumnas_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
            if (GrdColumnas.Columns[e.ColumnIndex].Name != null)
            {
                // Se toma la fila seleccionada 
                if (e.RowIndex > -1)
                {
                    DataGridViewRow row = GrdColumnas.Rows[e.RowIndex];
                    EBColumna co = new EBColumna();
                    co.Orden = Convert.ToInt32(row.Cells[1].Value.ToString());
                    co.CodigoAtributo = row.Cells[2].Value.ToString();
                    co.Nombres = row.Cells[3].Value.ToString();
                    co.FlagEliminado = "0";
                    ColumMod.Nuevo(co);
                }
            }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void GrdAtributos_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (GrdAtributos.IsCurrentCellDirty)
            {
                GrdAtributos.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }

        private void GrdColumnas_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (GrdColumnas.CurrentCell.ColumnIndex == 0)
            {
                if (GrdColumnas.IsCurrentCellDirty)
                {
                    GrdColumnas.CommitEdit(DataGridViewDataErrorContexts.Commit);
                }
            
            }

        }

        private void WFConfColumnas_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (elimina == true)
            {
                DialogResult = DialogResult.OK;
            }
            else
            {
                DialogResult = DialogResult.Cancel;
            }
        }
    }
}

